import Taro from '@tarojs/taro';
import { cacheService } from '.';

const coreService = {}
declare let THEME: string;
declare let STORE_ID: number;
declare let THEME_COLOR: string;

export function set(key, val) {
  coreService[key] = val
}

export function get(key) {
  return coreService[key]
}

export function del(key) {
  delete coreService[key]
}

export function compareTimeTypeOne(elementArray: Array<any>) {
  for (let i = 0; i < elementArray.length - 1; i++) {
    if (elementArray[i].showTime) {
      if (new Date().getTime() < new Date(elementArray[i].showTime.replace(/-/g, '/')).getTime()) {
        elementArray.splice(i, 1);
      }
    }
  }
}

export function compareTimeTypeAll(elementModel: any) {
  if (elementModel.showTime && new Date().getTime() < new Date(elementModel.showTime.replace(/-/g, '/')).getTime()) {
    elementModel.hidden = true
  }
}

export function getWebClass() {
  if (Taro.getEnv() == Taro.ENV_TYPE.WEB) {
    return 'web-margin-bottom-bar'
  } else {
    return ''
  }
}

export function getTheme() {
  // if (Taro.getEnv() == Taro.ENV_TYPE.WEAPP) {
  //   let config: any = Taro.getExtConfigSync()
  //   if (config && config.theme) {
  //     return config.theme;
  //   }
  //   return THEME;
  // } else {
  //   return THEME;
  // }
  return ""
}

export function getThemeColor() {
  let config: any = Taro.getExtConfigSync();
  if (config && config.themeColor) {
    return config.themeColor;
  }
  return THEME_COLOR;
}


export function getStoreId() {
  if (Taro.getEnv() == Taro.ENV_TYPE.WEAPP) {
    let config: any = Taro.getExtConfigSync()
    if (config && config.storeId) {
      return config.storeId;
    }
    if (typeof STORE_ID != 'undefined') {
      return STORE_ID;
    }
    return 0;
  } else {
    return 0
  }
}

//获取参数
export function getQueryVariable(variable, scene?) {
  var query = scene ? decodeURIComponent(scene) : this.$router.params.query;
  if (scene) {
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) { return pair[1]; }
    }
  } else {
    return query[variable];
  }
  return "";
}

export function formatLastTime(date: any): any {
  let nowTime: any = new Date().getTime();
  date = date.substring(0, 19);
  date = date.replace(/-/g, '/');
  let time: any = new Date(date).getTime();
  let resTime = (nowTime - time) / 1000;
  let hour48 = 48 * 60 * 60;
  // let day = Math.floor(resTime / (24 * 60 * 60));
  let hour = Math.floor((hour48 - resTime) / (60 * 60))
  let minute = Math.floor((hour48 - resTime) / (60));
  let second = Math.floor((hour48 - resTime));
  let returnStr = { value: "", unit: "" };
  if (resTime >= hour48) {
    returnStr.value = "0";
    return returnStr;
  }
  // if (day > 0) {
  //   returnStr = day.toString();
  //   return returnStr;
  // }
  if (hour > 0) {
    returnStr.value = hour.toString();
    returnStr.unit = "小时"
    return returnStr;
  }
  if (minute > 0) {
    returnStr.value = minute.toString();
    returnStr.unit = "分钟"
    return returnStr;
  }
  if (second > 0) {
    returnStr.value = second.toString();
    returnStr.unit = "秒"
    return returnStr;
  }
  return returnStr
}

export function throttle(fn: any, gapTime: number = 1500): any {
  // if (gapTime == null || gapTime === undefined) {
  //   gapTime = 1500
  // }
  let _lastTime = 0
  return function () {
    let _nowTime = +new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      // 将this和参数传给原函数
      fn.apply(this, arguments)
      _lastTime = _nowTime
    }
  }
}

/**
   * 解析参数或场景值中的参数
   */
export function praseParams($router) {
  let query = $router.params.query || $router.params;
  let shopId: number = 0;
  let goodsId: number = 0;
  let inviteCustomerId: number = 0;
  if (query && query.shopId) {
    shopId = query.shopId;
  }
  if (query && query.goodsId) {
    goodsId = query.goodsId;
  }
  if (query && query.customerId) {
    inviteCustomerId = query.customerId;
  }
  let scene = (query && query.scene) || $router.params.scene;
  if (scene) {
    let options: Array<string> = decodeURIComponent(scene).split('&');
    for (let option of options) {
      let keyValue: Array<string> = option.split("=");
      if (keyValue[0] == 'shopId') {
        shopId = +keyValue[1];
      } else if (keyValue[0] == 'goodsId') {
        goodsId = +keyValue[1];
      } else if (keyValue[0] == 'customerId') {
        inviteCustomerId = +keyValue[1];
      }
    }
  }
  return [shopId, goodsId, inviteCustomerId]
}
